// pages/my/my.js


const mdCode = require('../../utils/md5.js');
const desCode = require('../../utils/aesUtil.js');
const api_js = require("../../utils/api.js");
var app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {

    bar_Height: wx.getSystemInfoSync().statusBarHeight,

    isIphoneX_: app.globalData.isIphoneX,

    // 退出登录
    exit: false,

    // 是否退出弹层
    isExit: false,

    // 姓名
    nickName: "",
    // 头像
    headportrait: "",
    // 身份
    status: 1,


  },


  // 点击退出登录
  exitLoginFun() {
    this.setData({
      isExit: true,
    });
  },

  // 点击登录
  LoginFun() {
    wx.navigateTo({
      url: '../login/login'
    })
  },

  // 取消退出
  noExitFun() {
    this.setData({
      isExit: false,
    });
  },

  // 确认退出
  okExitFun() {
    this.setData({
      isExit: false,
    });
    var wMsgID = desCode.wMsgID();
    var wParam = desCode.to3des(`userid=${app.globalData.UserID}`);
    var md = mdCode.hexMD5('9999' + '104' + wMsgID + wParam + 'q1w2e3r4t5y6');
    const data = {
      wAgent: 9999,
      wAction: 104,
      wMsgID: wMsgID,
      wParam: wParam,
      wSign: md,
      wImei: 222,
      wVersion: 2,
      wRequestUserID: 4
    }
    api_js.postReq(data, (res) => {
      console.log(res, "退出登录")
      if (res.ReturnCode == 0) {
        app.globalData.LoginStatus = 1;
        app.globalData.UserID = '';
        app.globalData.Phone = '';
        app.globalData.UserName = '';
        app.globalData.HeadPortrait = '';
        app.globalData.Status = 1;
        wx.removeStorageSync('UserID')
        this.setData({
          exit: false,
        });
        wx.reLaunch({
          url: '../index/index'
        })
        wx.showToast({
          title: '退出成功',
          icon: 'none',
          duration: 1500,
          mask: true,
        })
      }
    })
  },

  // 查看签到状态
  checkStateFun() {
    if (app.globalData.LoginStatus == 1) {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000,
        mask: true,
      })
      return;
    }

    wx.navigateTo({
      url: '../checkState/checkState'
    })
  },





  ifNetwork() {
    wx.getNetworkType({
      success: function (res) {
        if (res.networkType == 'none') {
          wx.showModal({
            title: '网络错误',
            content: '网络出错，请刷新重试',
            showCancel: false
          });
        }
      }
    })
  },


  // 授权
  bindGetUserInfo(e) {
    if (app.globalData.LoginStatus == 1) {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000,
        mask: true,
      })
      return;
    } else {
      if (e.detail.rawData) {
        //用户按了允许授权按钮
        console.log(e.detail, "e.detail")


        //   入参：
        //   userid ///用户id
        // phone ///手机号码
        // openid ///openid
        // nlikename ///昵称
        // headportrait ///头像
        // gender ///性别

        let userid = app.globalData.UserID;
        let phone = app.globalData.Phone;
        let openid = app.globalData.openid;
        let nickName = e.detail.userInfo.nickName.replace(/_/g, "^=^");
        let headportrait = e.detail.userInfo.avatarUrl.replace(/_/g, "^=^");
        let gender = e.detail.userInfo.gender;

        console.log(userid, "userid");
        console.log(phone, "phone");
        console.log(openid, "openid");
        console.log(nickName, "nickName");
        console.log(headportrait, "headportrait");
        console.log(gender, "gender");






        var wMsgID = desCode.wMsgID();
        var wParam = desCode.to3des(`userid=${userid}_phone=${phone}_openid=${openid}_nlikename=${nickName}_headportrait=${headportrait}_gender=${gender}`);
        var md = mdCode.hexMD5('9999' + '113' + wMsgID + wParam + 'q1w2e3r4t5y6');
        const data = {
          wAgent: 9999,
          wAction: 113,
          wMsgID: wMsgID,
          wParam: wParam,
          wSign: md,
          wImei: 222,
          wVersion: 2,
          wRequestUserID: 4
        }
        api_js.postReq(data, (res) => {


          console.log(app.globalData.UserID, "app.globalData.UserID");
          console.log(app.globalData.LoginStatus, "this.globalData.LoginStatus");
          console.log(app.globalData.Phone, "app.globalData.Phone");
          console.log(app.globalData.UserName, "app.globalData.UserName");
          console.log(app.globalData.HeadPortrait, "app.globalData.HeadPortrait");


          console.log(res, "点击登录");

          // 106接口
          // userid



          var wMsgID = desCode.wMsgID();
          var wParam = desCode.to3des(`userid=${app.globalData.UserID}`);
          var md = mdCode.hexMD5('9999' + '106' + wMsgID + wParam + 'q1w2e3r4t5y6');
          const data = {
            wAgent: 9999,
            wAction: 106,
            wMsgID: wMsgID,
            wParam: wParam,
            wSign: md,
            wImei: 222,
            wVersion: 2,
            wRequestUserID: 4
          }
          api_js.postReq(data, (res) => {
            console.log(res, "我的个人中心");

            app.globalData.UserName = res.Data[0].nlikename;
            app.globalData.HeadPortrait = res.Data[0].headportrait;


            this.setData({
              nickName: app.globalData.UserName,
              headPortrait: app.globalData.HeadPortrait,
            });

          })

        })








      } else {
        //用户按了拒绝按钮
        console.log("被拒绝")
      }
    }

  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {



    if (app.globalData.Status == 2) {
      app.editTabBar2();
    } else {
      app.editTabBar3();
    }

    if (app.globalData.LoginStatus == 0) {
      this.setData({
        exit: true
      });

      this.setData({
        nickName: app.globalData.UserName,
        headPortrait: app.globalData.HeadPortrait,
        status: app.globalData.Status,
      });

    } else {
      this.setData({
        exit: false
      });
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '媛月幸福汇',
      path: '/pages/index/index?isShare=1',
      imageUrl: '/images/DefaultAvatar.png',
    }
  },
})